package com.abocide.appium.sdk.factory;

import com.abocide.appium.sdk.core.SdkDriver;
import io.appium.java_client.AppiumDriver;
import io.appium.java_client.android.AndroidDriver;
import org.openqa.selenium.remote.DesiredCapabilities;

import java.net.MalformedURLException;
import java.net.URL;

public class SdkDriverBuilder {

    public static Builder getBuilder() {
        return new Builder();
    }

    public static class Builder {
        private String host;
        private String port;
        private DesiredCapabilities capabilities;

        public Builder() {
            this.host = "127.0.0.1";
            this.port = "4723";
            this.capabilities = capabilities = new DesiredCapabilities();
            capabilities.setCapability("noReset", "true");
            capabilities.setCapability("adbExecTimeout", 9000000);
            capabilities.setCapability("automationName", "UiAutomator2");
        }

        public Builder host(String host) {
            this.host = host;
            return this;
        }

        public Builder port(String port) {
            this.port = port;
            return this;
        }

        public Builder platformName(Object value) {
            this.capabilities.setCapability("platformName", value);
            return this;
        }

        public Builder platformVersion(Object value) {
            this.capabilities.setCapability("platformVersion", value);
            return this;
        }

        public Builder deviceName(Object value) {
            this.capabilities.setCapability("deviceName", value);
            return this;
        }

        public Builder udid(Object value) {
            this.capabilities.setCapability("udid", value);
            return this;
        }

        public Builder appPackage(Object value) {
            this.capabilities.setCapability("appPackage", value);
            return this;
        }

        public Builder appActivity(Object value) {
            this.capabilities.setCapability("appActivity", value);
            return this;
        }

        public Builder addArgs(String key, Object value) {
            this.capabilities.setCapability(key, value);
            return this;
        }

        public SdkDriver build() throws MalformedURLException {
            return new SdkDriver(new AndroidDriver(new URL("http://" + this.host + ":" + this.port + "/wd/hub"),
                    capabilities));
        }
    }


}
